﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Settings;

namespace PanasonicWarranty.Business
{
    public partial class pa_CustomerGroup
    {
        public static pa_CustomerGroup GetByCode(string customerGroupCode)
        {
            using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
            {
                var retValue = context.pa_CustomerGroups.FirstOrDefault(p => p.CustomerGroupCode == customerGroupCode);
                if (retValue == null)
                {
                    retValue = new pa_CustomerGroup()
                    {
                        CustomerGroupCode = customerGroupCode,
                        CustomerGroupName = customerGroupCode
                    };
                    context.pa_CustomerGroups.InsertOnSubmit(retValue);
                    context.SubmitChanges();
                }
                return retValue;
            }
        }

        public static List<pa_CustomerGroup> GetAll()
        {
            using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
            {
                return context.pa_CustomerGroups.OrderBy(p => p.OrderID).ToList();
            }
        }

        public static pa_CustomerGroup GetByID(int Id)
        {
            using (var context = new DBWarrantyDataContext(AppSetting.ConnectionString))
            {
                return context.pa_CustomerGroups.FirstOrDefault(p => p.CustomerGroupID == Id);
            }
        }

        public static string GetName(object Id)
        {
            try
            {
                if (Id == null) return "";
                var obj = GetByID(Convert.ToInt32(Id));
                if (obj != null)
                    return obj.CustomerGroupName;
                return "";
            }
            catch
            {
                return "";
            }
        }
    }
}
